<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table @row-dblclick="showDetails" :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="120">
			</el-table-column>
			<el-table-column type="index" width="120">
			</el-table-column>
			<el-table-column prop="name" label="字典类型名" width="300" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="字典类型编号" width="300" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>

		</el-table>

		<!--抽屉-->
		<el-drawer
				title="明细"
				:visible.sync="drawer"
				:before-close="handleClose"
				size="50%">
			<detail></detail>
		</el-drawer>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--修改或者新增界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典类型名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典类型编号" prop="name">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="上级部门">
					<el-cascader v-model="editForm.parent"
							:options="empTree"
							:props="{
								checkStrictly: true,
								label: 'name',//双引号不行
								value: 'id'
							}"
							clearable>
					</el-cascader>
				</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>


</template>

<script>
    import detail from "@/views/system/Systemdictionarydetail.vue"

    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                drawer:false,
                types: [],
                /*empTree: [],*/
                total: 0,
                currentPage: 1,
                pageSize: 10,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入明细名', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择类型', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: null,
                    name: '',
                    sn:""
                },

            }
        },
        methods: {
            showDetails(row){
				this.$http.get("/detail/setSign/"+row.id)
					.then(result => {
                        this.drawer=true;
                    })
			},

            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                        //刷新页面
                        location.reload();
                    })
                    .catch(_ => {});
            },

            search(){
                this.currentPage = 1;
                this.getTypes();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getTypes();
            },
            //获取部门列表
            getTypes() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;

                this.$http.post("/type/page",para).then(result => {
                    this.listLoading = false;
                    this.types = result.data.rows;
                    this.total = result.data.totals;
                })

            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();

                    /*removeTypes(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTypes();
                    });*/
                    //发送请求
                    this.$http.delete("/type/delete/"+row.id)
                        .then(result => {
                            this.listLoading = false;
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                //定位到第一页
                                this.currentPage = 1;
                                this.getTypes();
                            }else {
                                this.listLoading = false;
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统错误，正在殴打程序员",
                                type: 'error'
                            });
                        })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                /*let arr = row.dirPath.split("/");
                let tmp = [];
                for (let i = 1; i < arr.length-1; i++) {
                    tmp.push(parseInt(arr[i]));
                }
                this.editForm.parent = tmp;*/

            },
            //显示新增界面
            handleAdd: function () {
                this.editFormVisible = true;
                this.editForm = {
                    id: null,
                    name: '',
                    sn:""
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            //格式化参数TODO
                            /*para.parent = {id:para.parent[para.parent.length - 1]};*/
                            //发送请求
                            this.$http.post("/type/save",para)
                                .then(result => {
                                    this.editLoading = false;
                                    result = result.data;
                                    if(result.success){
                                        this.$message({
                                            message: result.message,
                                            type: 'success'
                                        });
                                        //重置检验
                                        this.$refs['editForm'].resetFields();
                                        //关闭模态框
                                        this.editFormVisible = false;
                                        //刷新页面
                                        this.getTypes();
                                    }else {
                                        this.editLoading = false;
                                        this.$message({
                                            message: result.message,
                                            type: 'error'
                                        });
                                    }
                                })
                                .catch(result => {
                                    this.editLoading = false;
                                    this.$message({
                                        message: "系统错误，正在殴打程序员",
                                        type: 'error'
                                    });
                                })

                            /*editTypes(para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getTypes();
                            });*/
                        });
                    }
                });
            },

            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    /*batchRemoveTypes(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTypes();
                    });*/
                    this.$http.patch("/type/batch",ids)
                        .then(result => {
                            this.listLoading = false;
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                //定位到第一页
                                this.currentPage = 1;
                                this.getTypes();
                            }else {
                                this.listLoading = false;
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统错误，正在殴打程序员",
                                type: 'error'
                            });
                        })
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getTypes();
            /*this.getDepartments();*/
        },
		components:{
            detail
		}
    }

</script>

<style scoped>

</style>